#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#define STACK_INIT_SIZE 100
#define STACK_INCREMENT 10
#define ERROR -1
#define OK 1
#define OVERFLOW -2
typedef int Status;
typedef int ElemType;
typedef struct
{
    ElemType* base;
    ElemType* top;
    int stackSize;
}SqStack;
Status InitStack(SqStack &Sq)
{
    Sq.base = (ElemType*)malloc(sizeof(ElemType) * STACK_INIT_SIZE);
    if (!Sq.base)
        exit(OVERFLOW);
    Sq.top = Sq.base;
    Sq.stackSize = STACK_INIT_SIZE;
    return OK;
}
Status Push(SqStack &Sq, ElemType e)
{
    if (Sq.top - Sq.base >= Sq.stackSize)
    {
        Sq.base = (ElemType*)realloc(Sq.base, sizeof(ElemType) * (Sq.stackSize + STACK_INCREMENT));
        if (!Sq.base)
            exit(OVERFLOW);
        Sq.top = Sq.base + Sq.stackSize;
        Sq.stackSize += STACK_INCREMENT;
    }
    *(Sq.top++) = e;
    return OK;
}
Status Pop(SqStack &Sq, ElemType &e)
{
    if (Sq.base == Sq.top)
        return ERROR;
    e = *(--Sq.top);
    return OK;
}
Status GetTop(SqStack Sq, ElemType &e)
{
    if (Sq.base == Sq.top)
        return ERROR;
    e = *(Sq.top - 1);
    return OK;
}
Status showStack(SqStack Sq)
{
    int len = Sq.top - Sq.base;
    printf("total: %d\n", len);
    for (int i = 0; i < len; ++i)
        printf("%d ", Sq.base[i]);
    printf("\n");
    return OK;
}
int main(void)
{
    SqStack s;
    InitStack(s);
    showStack(s);
    ElemType e = 3;
    Push(s, e);
    e = 5;
    Push(s, e);
    showStack(s);
    e = 4;
    Pop(s, e);
    printf("e = %d\n", e);
    showStack(s);
    GetTop(s, e);
    printf("e = %d\n", e);

    return 0;
}